package manage

import "strings"

func Contains(target string, array []string) bool {
	var contain = false
	targetStr := removeEmpty(strings.Split(target, "/"))
	targetLen := len(targetStr)
	for _, v := range array {
		match := false

		if target==v{
			match=true;
			contain=true
			break
		}

		splitString := removeEmpty(strings.Split(v, "/"))
		for i := 0; i < len(splitString); i++ {
			if i < targetLen {
				if splitString[i] == "*" || splitString[i] == "**" {
					match = true
				} else if splitString[i] == targetStr[i] {
					match = true
				} else {
					match = false
					break
				}

			} else {
				if strings.HasSuffix(v, "**") {
					match = true
				}
				match = false
				break
			}
		}
		if match {
			contain = true
			break
		}
	}
	return contain
}

func removeEmpty(targets []string) []string {
	var result []string
	for _, v := range targets {
		if v != "" {
			result = append(result, v)
		}
	}
	return result
}
